using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Security
{
    /// <summary>
    /// 
    /// </summary>
    public class SimpleIdentity : System.Security.Principal.IIdentity 
    {
        private string _UserName;
        /// <summary></summary>
        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }
        private string _Password = string.Empty;
        /// <summary></summary>
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }
        private DBAuthenticationType _AuthType;
        private bool _Authenticated = false;
        /// <summary></summary>
        public SimpleIdentity(string username)
        {
            this._UserName = username;
            this._AuthType = DBAuthenticationType.SQLServerAuthentication;
            this._Authenticated = this.TryWindowsAuthentication();
        }
        /// <summary></summary>
        public SimpleIdentity(string username, string password)
        {
            this._UserName = username;
            this._Password = password;
            if (!string.IsNullOrEmpty(password))
            {
                this._AuthType = DBAuthenticationType.SQLServerAuthentication;
                this._Authenticated = this.TrySqlAuthentication();
            }
            else
            {
                this._AuthType = DBAuthenticationType.WindowsAuthentication;
                this._Authenticated = this.TryWindowsAuthentication();
            }
        }
        

        #region IIdentity Members

        /// <summary>
        /// 
        /// </summary>
        public string AuthenticationType
        {
            get
            {
                if (this._AuthType == DBAuthenticationType.WindowsAuthentication)
                    return "Windows Integrated Authentication";
                else
                    return "SQL Authentication";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool IsAuthenticated
        {
            get 
            {
                return this._Authenticated;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Name
        {
            get
            {
                return this._UserName;
            }
        }

        #endregion

        #region authenticate
        private bool TryWindowsAuthentication()
        {
            return true;
            //string pattern = this._UserName.Substring(0, 1) + "*";
            //List<string> users = LdapUtil.GetUserNames(pattern);
            //if (users.Contains(this._UserName))
            //    return true;
            //else
            //    return false;
        }

        private bool TrySqlAuthentication()
        {
            if (this._UserName.ToLower() == "xiaodongl" && this._Password == "Summer1")
                return true;
            else
                return false;
        }
        #endregion
    }
}
